package cn.itsource.xhy.pet.service.impl;

import cn.itsource.xhy.basic.domain.Point;
import cn.itsource.xhy.basic.query.BaseQuery;
import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.mapper.ShopMapper;
import cn.itsource.xhy.pet.domain.SearchMasterMsg;
import cn.itsource.xhy.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.xhy.pet.query.SearchMasterMsgQuery;
import cn.itsource.xhy.pet.service.ISearchMasterMsgService;
import cn.itsource.xhy.user.domain.LoginInfo;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.utils.DistanceUtil;
import cn.itsource.xhy.utils.LoginContext;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        // 获取登录用户
        User user = (User) LoginContext.getObject(request);
        // 将user设置进去
        searchMasterMsg.setUser(user);
        // 使用工具类将地址转换为经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 获取门店信息
        List<Shop> shops = shopMapper.findAll();
        // 使用工具类获取直线距离最近的门店
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        // 将门店设置进去
        searchMasterMsg.setShop(shop);
        // 设置状态为代审核
        searchMasterMsg.setState(0);
        // 保存信息
        super.save(searchMasterMsg);
    }

    @Override
    public QueryResult<SearchMasterMsg> queryPage(SearchMasterMsgQuery searchMasterMsgQuery) {
        return super.queryPage(searchMasterMsgQuery);

    }
}
